// pages/choosemusic/choosemusic.js
const app = getApp()
var server = require('../../utils/server');
const innerAudioContext = wx.createInnerAudioContext();
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choiceid: '1',
    bgmname:'无',
    bgmurl:'',
    bgmlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    that = this

    var bgmlist = JSON.parse(e.bgmlist)
    var cc = {
      _id: "",
      name: "无",
      url: "",
      // bgm:''
    }
    bgmlist.unshift(cc)
    console.log('bgmlist',bgmlist)
    that.setData({
      bgmlist:bgmlist,
      choiceid:bgmlist[0]._id
    }) 
    // innerAudioContext.autoplay = true;
    // innerAudioContext.src = bgmlist[0].url;
    // innerAudioContext.loop = true;
    // innerAudioContext.play();
  },
  onUnload:function(e){
    console.log('返回事件')
    innerAudioContext.stop();
  },

  click: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    var mp3 = e.currentTarget.dataset.mp3
    var name = e.currentTarget.dataset.name
    that.setData({
      choiceid: id,
      bgmurl:mp3,
      bgmname:name
    })
    if(!mp3){
      innerAudioContext.stop();
    }else{
      innerAudioContext.autoplay = true;
      innerAudioContext.src = mp3;
      innerAudioContext.loop = true;
      innerAudioContext.play();
    }
 
  },
  back:function(e){
    var pages = getCurrentPages(); // 获取页面栈
    var currPage = pages[pages.length - 1]; // 当前页面
    var prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.setData({
      bgmname: that.data.bgmname,
      bgmurl:that.data.bgmurl,
      sound1:''
    })
    console.log(prevPage)
    innerAudioContext.stop();
    wx.navigateBack({
      delta: 1
    })
  }
})